package com.test.mchelepesa.activity_fragment_rice;

import static android.Manifest.permission.READ_SMS;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.annotation.NonNull;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseInfoActivity_rice;
import com.test.mchelepesa.dialog_rice.TipsDialog_rice;
import com.test.mchelepesa.util_rice.PermissionUtil_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.analytics_rice.util_rice.AnalyticsUtil_rice;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.view_rice.widget_rice.TitleView_rice;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class PermissionActivity_rice extends BaseInfoActivity_rice {

    private final int CodePermissions = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_rice);

        TitleView_rice titleView_rice = findViewById(R.id.permission_title_view);
        CheckBox readCb = findViewById(R.id.permission_cb_read);
        Button denyBtn = findViewById(R.id.permission_btn_deny);
        Button agreeBtn = findViewById(R.id.permission_btn_agree);

        titleView_rice.getBackIb().setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("PERMISSION_BACK_CLICK");
            onBackPressed();
        });

        denyBtn.setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("PERMISSION_DENY_CLICK");
            next();
        });

        agreeBtn.setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("PERMISSION_AGREE_CLICK");
            if (readCb.isChecked())
                EasyPermissions.requestPermissions(this,getString(R.string.launch_permissions_camera),
                        CodePermissions, READ_SMS,Manifest.permission.CAMERA);
            else new TipsDialog_rice(this,() -> {
                readCb.setChecked(true);
                EasyPermissions.requestPermissions(this,getString(R.string.launch_permissions_camera),
                        CodePermissions, READ_SMS,Manifest.permission.CAMERA);
            }).show();
        });

    }


    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (isClose()) return;
        if (EasyPermissions.somePermissionPermanentlyDenied(this,perms)){
            new AppSettingsDialog.Builder(this).build().show();
        }else if (requestCode == CodePermissions) next();
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (isClose()) return;
        if (requestCode == CodePermissions) next();
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (isClose()) return;
        if (requestCode == CodePermissions){
            EasyPermissions.requestPermissions(this,getString(R.string.launch_permissions_camera),
                    CodePermissions, READ_SMS,Manifest.permission.CAMERA);
        }
    }

    private void next(){

        if (UserInfoHelper_rice.getInstance().isAdSource_rice()){

            if (PermissionUtil_rice.checkLaunchPermissions(this,READ_SMS)){

                try {
                    AnalyticsUtil_rice.INSTANCE.saveDeviceS2SInfo();
                } catch (Exception e) {
                    StringUtil_rice.printException(e);
                }

                HttpManager_rice.getInstance().requestUserProcess();

            }else finish();

        }else {

            try {
                AnalyticsUtil_rice.INSTANCE.saveDeviceS2SInfo();
            } catch (Exception e) {
                StringUtil_rice.printException(e);
            }

            HttpManager_rice.getInstance().requestUserProcess();

        }

    }

}